{
 "cells": [
  {
   "cell_type": "code",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-28T01:37:32.912131Z",
     "start_time": "2025-02-28T01:37:28.328551Z"
    }
   },
   "source": [
    "import time\n",
    "\n",
    "from sklearn.datasets import load_iris, fetch_20newsgroups, fetch_california_housing\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.metrics import roc_auc_score"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "load直接加载的内存的，数据集比较小，并不会保存到本地磁盘\n",
    "fetch数据集比较大，下载下来后会存在本地磁盘，下一次就不会再连接sklearn的服务器\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "#鸢尾花数据集，查看特征，目标，样本量\n",
    "\n",
    "li = load_iris()\n",
    "\n",
    "print(\"获取特征值\")\n",
    "print(type(li.data))\n",
    "print('-' * 50)\n",
    "print(li.data.shape) # 150个样本，4个特征,一般看shape\n",
    "li.data[:5]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-27T08:07:21.025006Z",
     "start_time": "2025-02-27T08:07:21.021094Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取特征值\n",
      "<class 'numpy.ndarray'>\n",
      "--------------------------------------------------\n",
      "(150, 4)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2],\n",
       "       [4.9, 3. , 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.3, 0.2],\n",
       "       [4.6, 3.1, 1.5, 0.2],\n",
       "       [5. , 3.6, 1.4, 0.2]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "source": [
    "print(\"目标值\")\n",
    "print(li.target)\n",
    "print('-' * 50)\n",
    "print(li.DESCR) #比较多\n",
    "print('-' * 50)\n",
    "print(li.feature_names)  # 重点,特征名字\n",
    "print('-' * 50)\n",
    "print(li.target_names) # 目标名字"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-27T08:10:15.822605Z",
     "start_time": "2025-02-27T08:10:15.819602Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标值\n",
      "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
      " 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2\n",
      " 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n",
      " 2 2]\n",
      "--------------------------------------------------\n",
      ".. _iris_dataset:\n",
      "\n",
      "Iris plants dataset\n",
      "--------------------\n",
      "\n",
      "**Data Set Characteristics:**\n",
      "\n",
      ":Number of Instances: 150 (50 in each of three classes)\n",
      ":Number of Attributes: 4 numeric, predictive attributes and the class\n",
      ":Attribute Information:\n",
      "    - sepal length in cm\n",
      "    - sepal width in cm\n",
      "    - petal length in cm\n",
      "    - petal width in cm\n",
      "    - class:\n",
      "            - Iris-Setosa\n",
      "            - Iris-Versicolour\n",
      "            - Iris-Virginica\n",
      "\n",
      ":Summary Statistics:\n",
      "\n",
      "============== ==== ==== ======= ===== ====================\n",
      "                Min  Max   Mean    SD   Class Correlation\n",
      "============== ==== ==== ======= ===== ====================\n",
      "sepal length:   4.3  7.9   5.84   0.83    0.7826\n",
      "sepal width:    2.0  4.4   3.05   0.43   -0.4194\n",
      "petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)\n",
      "petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)\n",
      "============== ==== ==== ======= ===== ====================\n",
      "\n",
      ":Missing Attribute Values: None\n",
      ":Class Distribution: 33.3% for each of 3 classes.\n",
      ":Creator: R.A. Fisher\n",
      ":Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n",
      ":Date: July, 1988\n",
      "\n",
      "The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\n",
      "from Fisher's paper. Note that it's the same as in R, but not as in the UCI\n",
      "Machine Learning Repository, which has two wrong data points.\n",
      "\n",
      "This is perhaps the best known database to be found in the\n",
      "pattern recognition literature.  Fisher's paper is a classic in the field and\n",
      "is referenced frequently to this day.  (See Duda & Hart, for example.)  The\n",
      "data set contains 3 classes of 50 instances each, where each class refers to a\n",
      "type of iris plant.  One class is linearly separable from the other 2; the\n",
      "latter are NOT linearly separable from each other.\n",
      "\n",
      ".. dropdown:: References\n",
      "\n",
      "  - Fisher, R.A. \"The use of multiple measurements in taxonomic problems\"\n",
      "    Annual Eugenics, 7, Part II, 179-188 (1936); also in \"Contributions to\n",
      "    Mathematical Statistics\" (John Wiley, NY, 1950).\n",
      "  - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n",
      "    (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.\n",
      "  - Dasarathy, B.V. (1980) \"Nosing Around the Neighborhood: A New System\n",
      "    Structure and Classification Rule for Recognition in Partially Exposed\n",
      "    Environments\".  IEEE Transactions on Pattern Analysis and Machine\n",
      "    Intelligence, Vol. PAMI-2, No. 1, 67-71.\n",
      "  - Gates, G.W. (1972) \"The Reduced Nearest Neighbor Rule\".  IEEE Transactions\n",
      "    on Information Theory, May 1972, 431-433.\n",
      "  - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al\"s AUTOCLASS II\n",
      "    conceptual clustering system finds 3 classes in the data.\n",
      "  - Many, many more ...\n",
      "\n",
      "--------------------------------------------------\n",
      "['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n",
      "--------------------------------------------------\n",
      "['setosa' 'versicolor' 'virginica']\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-10T02:00:54.646893Z",
     "start_time": "2025-01-10T02:00:54.643773Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(li.data.shape)\n",
    "li.target.shape"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(150, 4)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(150,)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "source": [
    "# 注意返回值, 训练集 train  x_train, y_train        测试集  test   x_test, y_test，顺序千万别搞错了\n",
    "# 默认是乱序的,random_state为了确保两次的随机策略一致，就会得到相同的随机数据，往往会带上\n",
    "x_train, x_test, y_train, y_test = train_test_split(li.data, li.target, test_size=0.25, random_state=1)\n",
    "\n",
    "print(\"训练集特征值和目标值：\", x_train.shape, y_train.shape)\n",
    "print(\"训练集特征值shape\", x_train.shape)\n",
    "print('-'*50)\n",
    "print(\"测试集特征值和目标值：\", x_test.shape, y_test.shape)\n",
    "print(\"测试集特征值shape\", x_test.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-27T08:12:07.764321Z",
     "start_time": "2025-02-27T08:12:07.760343Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集特征值和目标值： (112, 4) (112,)\n",
      "训练集特征值shape (112, 4)\n",
      "--------------------------------------------------\n",
      "测试集特征值和目标值： (38, 4) (38,)\n",
      "测试集特征值shape (38, 4)\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "source": [
    "150*0.25"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-10T02:00:54.653767Z",
     "start_time": "2025-01-10T02:00:54.651481Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "37.5"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "source": [
    "# 下面是比较大的数据，需要下载一会，20类新闻\n",
    "#subset代表下载的数据集类型，默认是train，只有训练集\n",
    "news = fetch_20newsgroups(subset='all', data_home='data')\n",
    "# print(news.feature_names)  #这个数据集是没有的，因为没有特征，只有文本数据\n",
    "# print(news.DESCR)\n",
    "print('第一个样本')\n",
    "print(news.data[0])\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T01:39:54.910279Z",
     "start_time": "2025-02-28T01:39:54.748381Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个样本\n",
      "From: Mamatha Devineni Ratnam <mr47+@andrew.cmu.edu>\n",
      "Subject: Pens fans reactions\n",
      "Organization: Post Office, Carnegie Mellon, Pittsburgh, PA\n",
      "Lines: 12\n",
      "NNTP-Posting-Host: po4.andrew.cmu.edu\n",
      "\n",
      "\n",
      "\n",
      "I am sure some bashers of Pens fans are pretty confused about the lack\n",
      "of any kind of posts about the recent Pens massacre of the Devils. Actually,\n",
      "I am  bit puzzled too and a bit relieved. However, I am going to put an end\n",
      "to non-PIttsburghers' relief with a bit of praise for the Pens. Man, they\n",
      "are killing those Devils worse than I thought. Jagr just showed you why\n",
      "he is much better than his regular season stats. He is also a lot\n",
      "fo fun to watch in the playoffs. Bowman should let JAgr have a lot of\n",
      "fun in the next couple of games since the Pens are going to beat the pulp out of Jersey anyway. I was very disappointed not to see the Islanders lose the final\n",
      "regular season game.          PENS RULE!!!\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T01:39:06.421403Z",
     "start_time": "2025-02-28T01:39:06.418506Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('特征类型')\n",
    "print(type(news.data))\n",
    "print('-' * 50)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征类型\n",
      "<class 'list'>\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T01:40:28.635965Z",
     "start_time": "2025-02-28T01:40:28.632964Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(news.target[0:30])\n",
    "from pprint import pprint\n",
    "pprint(list(news.target_names))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10  3 17  3  4 12  4 10 10 19 19 11 19 13  0 17 12 12 11  8  7  5  1  8\n",
      " 10 14 16  1  6  0]\n",
      "['alt.atheism',\n",
      " 'comp.graphics',\n",
      " 'comp.os.ms-windows.misc',\n",
      " 'comp.sys.ibm.pc.hardware',\n",
      " 'comp.sys.mac.hardware',\n",
      " 'comp.windows.x',\n",
      " 'misc.forsale',\n",
      " 'rec.autos',\n",
      " 'rec.motorcycles',\n",
      " 'rec.sport.baseball',\n",
      " 'rec.sport.hockey',\n",
      " 'sci.crypt',\n",
      " 'sci.electronics',\n",
      " 'sci.med',\n",
      " 'sci.space',\n",
      " 'soc.religion.christian',\n",
      " 'talk.politics.guns',\n",
      " 'talk.politics.mideast',\n",
      " 'talk.politics.misc',\n",
      " 'talk.religion.misc']\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T01:42:08.846125Z",
     "start_time": "2025-02-28T01:42:08.842131Z"
    }
   },
   "cell_type": "code",
   "source": "len(news.target_names)",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "source": [
    "print('-' * 50)\n",
    "print(len(news.data))\n",
    "print('新闻所有的标签')\n",
    "print(len(news.target))\n",
    "print('-' * 50)\n",
    "print(min(news.target), max(news.target))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-28T01:42:50.835546Z",
     "start_time": "2025-02-28T01:42:50.831543Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "18846\n",
      "新闻所有的标签\n",
      "18846\n",
      "--------------------------------------------------\n",
      "0 19\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "source": [
    "house=fetch_california_housing(data_home='data')\n",
    "print(\"获取特征值\")\n",
    "print(house.data[0])  #第一个样本特征值\n",
    "print('样本的形状')\n",
    "print(house.data.shape)\n",
    "print('-' * 50)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T01:44:15.196333Z",
     "start_time": "2025-02-28T01:44:15.181220Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取特征值\n",
      "[   8.3252       41.            6.98412698    1.02380952  322.\n",
      "    2.55555556   37.88       -122.23      ]\n",
      "样本的形状\n",
      "(20640, 8)\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "source": [
    "print(\"目标值\")\n",
    "print(house.target[0:10])\n",
    "print('-' * 50)\n",
    "# print(house.DESCR)\n",
    "print('-' * 50)\n",
    "print(house.feature_names)\n",
    "print('-' * 50)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-28T01:44:28.214292Z",
     "start_time": "2025-02-28T01:44:28.211292Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标值\n",
      "[4.526 3.585 3.521 3.413 3.422 2.697 2.992 2.414 2.267 2.611]\n",
      "--------------------------------------------------\n",
      "--------------------------------------------------\n",
      "['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-10T02:00:58.365906Z",
     "start_time": "2025-01-10T02:00:58.363100Z"
    }
   },
   "cell_type": "code",
   "source": "house.target.shape",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(20640,)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-10T02:01:25.331727Z",
     "start_time": "2025-01-10T02:01:25.328729Z"
    }
   },
   "cell_type": "code",
   "source": "house.data[0]",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   8.3252    ,   41.        ,    6.98412698,    1.02380952,\n",
       "        322.        ,    2.55555556,   37.88      , -122.23      ])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-10T02:01:38.747182Z",
     "start_time": "2025-01-10T02:01:38.744683Z"
    }
   },
   "cell_type": "code",
   "source": "house.target[0]",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.526"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2 分类估计器"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "20.518284528683193"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(15*15+14*14)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-04-17T02:41:10.593503700Z",
     "start_time": "2024-04-17T02:41:10.559525100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# K近邻\n",
    "\"\"\"\n",
    "K-近邻预测用户签到位置\n",
    ":return:None\n",
    "\"\"\"\n",
    "# 读取数据\n",
    "data = pd.read_csv(\"./data/FBlocation/train.csv\")\n",
    "\n",
    "print(data.head(10))\n",
    "print(data.shape)\n",
    "print(data.info())\n",
    "# 处理数据\n",
    "# 1、缩小数据,查询数据,为了减少计算时间\n",
    "data = data.query(\"x > 1.0 &  x < 1.25 & y > 2.5 & y < 2.75\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T01:56:56.093957Z",
     "start_time": "2025-02-28T01:56:49.400150Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   row_id       x       y  accuracy    time    place_id\n",
      "0       0  0.7941  9.0809        54  470702  8523065625\n",
      "1       1  5.9567  4.7968        13  186555  1757726713\n",
      "2       2  8.3078  7.0407        74  322648  1137537235\n",
      "3       3  7.3665  2.5165        65  704587  6567393236\n",
      "4       4  4.0961  1.1307        31  472130  7440663949\n",
      "5       5  3.8099  1.9586        75  178065  6289802927\n",
      "6       6  6.3336  4.3720        13  666829  9931249544\n",
      "7       7  5.7409  6.7697        85  369002  5662813655\n",
      "8       8  4.3114  6.9410         3  166384  8471780938\n",
      "9       9  6.3414  0.0758        65  400060  1253803156\n",
      "(29118021, 6)\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 29118021 entries, 0 to 29118020\n",
      "Data columns (total 6 columns):\n",
      " #   Column    Dtype  \n",
      "---  ------    -----  \n",
      " 0   row_id    int64  \n",
      " 1   x         float64\n",
      " 2   y         float64\n",
      " 3   accuracy  int64  \n",
      " 4   time      int64  \n",
      " 5   place_id  int64  \n",
      "dtypes: float64(2), int64(4)\n",
      "memory usage: 1.3 GB\n",
      "None\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "source": [
    "data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-28T02:00:37.873435Z",
     "start_time": "2025-02-28T02:00:37.869484Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(17710, 6)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "source": "data.describe() #有没有空",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-10T02:44:45.094182Z",
     "start_time": "2025-01-10T02:44:45.084309Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             row_id             x             y      accuracy           time  \\\n",
       "count  1.771000e+04  17710.000000  17710.000000  17710.000000   17710.000000   \n",
       "mean   1.450569e+07      1.122538      2.632309     82.482101  397551.263128   \n",
       "std    8.353805e+06      0.077086      0.070144    113.613227  234601.097883   \n",
       "min    6.000000e+02      1.000100      2.500100      1.000000     119.000000   \n",
       "25%    7.327816e+06      1.049200      2.573800     25.000000  174069.750000   \n",
       "50%    1.443071e+07      1.123300      2.642300     62.000000  403387.500000   \n",
       "75%    2.163463e+07      1.190500      2.687800     75.000000  602111.750000   \n",
       "max    2.911215e+07      1.249900      2.749900   1004.000000  786218.000000   \n",
       "\n",
       "           place_id  \n",
       "count  1.771000e+04  \n",
       "mean   5.129895e+09  \n",
       "std    2.357399e+09  \n",
       "min    1.012024e+09  \n",
       "25%    3.312464e+09  \n",
       "50%    5.261906e+09  \n",
       "75%    6.766325e+09  \n",
       "max    9.980711e+09  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.771000e+04</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>1.771000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.450569e+07</td>\n",
       "      <td>1.122538</td>\n",
       "      <td>2.632309</td>\n",
       "      <td>82.482101</td>\n",
       "      <td>397551.263128</td>\n",
       "      <td>5.129895e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.353805e+06</td>\n",
       "      <td>0.077086</td>\n",
       "      <td>0.070144</td>\n",
       "      <td>113.613227</td>\n",
       "      <td>234601.097883</td>\n",
       "      <td>2.357399e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>6.000000e+02</td>\n",
       "      <td>1.000100</td>\n",
       "      <td>2.500100</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>119.000000</td>\n",
       "      <td>1.012024e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.327816e+06</td>\n",
       "      <td>1.049200</td>\n",
       "      <td>2.573800</td>\n",
       "      <td>25.000000</td>\n",
       "      <td>174069.750000</td>\n",
       "      <td>3.312464e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.443071e+07</td>\n",
       "      <td>1.123300</td>\n",
       "      <td>2.642300</td>\n",
       "      <td>62.000000</td>\n",
       "      <td>403387.500000</td>\n",
       "      <td>5.261906e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.163463e+07</td>\n",
       "      <td>1.190500</td>\n",
       "      <td>2.687800</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>602111.750000</td>\n",
       "      <td>6.766325e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.911215e+07</td>\n",
       "      <td>1.249900</td>\n",
       "      <td>2.749900</td>\n",
       "      <td>1004.000000</td>\n",
       "      <td>786218.000000</td>\n",
       "      <td>9.980711e+09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T02:22:39.394793Z",
     "start_time": "2025-02-28T02:22:39.386584Z"
    }
   },
   "cell_type": "code",
   "source": "data #最前面是行索引，现在不连续了",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            row_id       x       y  accuracy    time    place_id\n",
       "600            600  1.2214  2.7023        17   65380  6683426742\n",
       "957            957  1.1832  2.6891        58  785470  6683426742\n",
       "4345          4345  1.1935  2.6550        11  400082  6889790653\n",
       "4735          4735  1.1452  2.6074        49  514983  6822359752\n",
       "5580          5580  1.0089  2.7287        19  732410  1527921905\n",
       "...            ...     ...     ...       ...     ...         ...\n",
       "29100203  29100203  1.0129  2.6775        12   38036  3312463746\n",
       "29108443  29108443  1.1474  2.6840        36  602524  3533177779\n",
       "29109993  29109993  1.0240  2.7238        62  658994  6424972551\n",
       "29111539  29111539  1.2032  2.6796        87  262421  3533177779\n",
       "29112154  29112154  1.1070  2.5419       178  687667  4932578245\n",
       "\n",
       "[17710 rows x 6 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>600</th>\n",
       "      <td>600</td>\n",
       "      <td>1.2214</td>\n",
       "      <td>2.7023</td>\n",
       "      <td>17</td>\n",
       "      <td>65380</td>\n",
       "      <td>6683426742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>957</th>\n",
       "      <td>957</td>\n",
       "      <td>1.1832</td>\n",
       "      <td>2.6891</td>\n",
       "      <td>58</td>\n",
       "      <td>785470</td>\n",
       "      <td>6683426742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4345</th>\n",
       "      <td>4345</td>\n",
       "      <td>1.1935</td>\n",
       "      <td>2.6550</td>\n",
       "      <td>11</td>\n",
       "      <td>400082</td>\n",
       "      <td>6889790653</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4735</th>\n",
       "      <td>4735</td>\n",
       "      <td>1.1452</td>\n",
       "      <td>2.6074</td>\n",
       "      <td>49</td>\n",
       "      <td>514983</td>\n",
       "      <td>6822359752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5580</th>\n",
       "      <td>5580</td>\n",
       "      <td>1.0089</td>\n",
       "      <td>2.7287</td>\n",
       "      <td>19</td>\n",
       "      <td>732410</td>\n",
       "      <td>1527921905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29100203</th>\n",
       "      <td>29100203</td>\n",
       "      <td>1.0129</td>\n",
       "      <td>2.6775</td>\n",
       "      <td>12</td>\n",
       "      <td>38036</td>\n",
       "      <td>3312463746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29108443</th>\n",
       "      <td>29108443</td>\n",
       "      <td>1.1474</td>\n",
       "      <td>2.6840</td>\n",
       "      <td>36</td>\n",
       "      <td>602524</td>\n",
       "      <td>3533177779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29109993</th>\n",
       "      <td>29109993</td>\n",
       "      <td>1.0240</td>\n",
       "      <td>2.7238</td>\n",
       "      <td>62</td>\n",
       "      <td>658994</td>\n",
       "      <td>6424972551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29111539</th>\n",
       "      <td>29111539</td>\n",
       "      <td>1.2032</td>\n",
       "      <td>2.6796</td>\n",
       "      <td>87</td>\n",
       "      <td>262421</td>\n",
       "      <td>3533177779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29112154</th>\n",
       "      <td>29112154</td>\n",
       "      <td>1.1070</td>\n",
       "      <td>2.5419</td>\n",
       "      <td>178</td>\n",
       "      <td>687667</td>\n",
       "      <td>4932578245</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>17710 rows × 6 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "source": [
    "# 处理时间的数据，unit是秒，把秒转换成日期格式\n",
    "time_value = pd.to_datetime(data['time'], unit='s')\n",
    "\n",
    "print(time_value.head(10))  #最大时间是1月10号"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-28T02:24:38.334937Z",
     "start_time": "2025-02-28T02:24:38.323205Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "600    1970-01-01 18:09:40\n",
      "957    1970-01-10 02:11:10\n",
      "4345   1970-01-05 15:08:02\n",
      "4735   1970-01-06 23:03:03\n",
      "5580   1970-01-09 11:26:50\n",
      "6090   1970-01-02 16:25:07\n",
      "6234   1970-01-04 15:52:57\n",
      "6350   1970-01-01 10:13:36\n",
      "7468   1970-01-09 15:26:06\n",
      "8478   1970-01-08 23:52:02\n",
      "Name: time, dtype: datetime64[ns]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "source": [
    "# 把日期格式转换成 索引格式\n",
    "time_value = pd.DatetimeIndex(time_value)\n",
    "#\n",
    "print('-' * 50)\n",
    "print(time_value[0:10])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:26:24.063039Z",
     "start_time": "2025-02-28T02:26:24.052316Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "DatetimeIndex(['1970-01-01 18:09:40', '1970-01-10 02:11:10',\n",
      "               '1970-01-05 15:08:02', '1970-01-06 23:03:03',\n",
      "               '1970-01-09 11:26:50', '1970-01-02 16:25:07',\n",
      "               '1970-01-04 15:52:57', '1970-01-01 10:13:36',\n",
      "               '1970-01-09 15:26:06', '1970-01-08 23:52:02'],\n",
      "              dtype='datetime64[ns]', name='time', freq=None)\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "source": [
    "data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:26:32.678669Z",
     "start_time": "2025-02-28T02:26:32.675390Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(17710, 6)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "source": [
    "print('-' * 50)\n",
    "# 构造一些特征，执行的警告是因为我们的操作是复制，loc是直接放入\n",
    "print(type(data))\n",
    "# data['day'] = time_value.day\n",
    "# data['hour'] = time_value.hour\n",
    "# data['weekday'] = time_value.weekday\n",
    "#日期，是否是周末，小时对于个人行为的影响是较大的(例如吃饭时间去饭店，看电影时间去电影院等),所以才做下面的处理\n",
    "data.insert(data.shape[1], 'day', time_value.day) #data.shape[1]是代表插入到最后的意思,一个月的哪一天\n",
    "data.insert(data.shape[1], 'hour', time_value.hour)#是否去一个地方打卡，早上，中午，晚上是有影响的\n",
    "data.insert(data.shape[1], 'weekday', time_value.weekday) #0代表周一，6代表周日，星期几\n",
    "\n",
    "#\n",
    "# 把时间戳特征删除\n",
    "data = data.drop(['time'], axis=1)\n",
    "print('-' * 50)\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:30:59.640058Z",
     "start_time": "2025-02-28T02:30:59.629139Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "--------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "      row_id       x       y  accuracy    place_id  day  hour  weekday\n",
       "600      600  1.2214  2.7023        17  6683426742    1    18        3\n",
       "957      957  1.1832  2.6891        58  6683426742   10     2        5\n",
       "4345    4345  1.1935  2.6550        11  6889790653    5    15        0\n",
       "4735    4735  1.1452  2.6074        49  6822359752    6    23        1\n",
       "5580    5580  1.0089  2.7287        19  1527921905    9    11        4"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>place_id</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>600</th>\n",
       "      <td>600</td>\n",
       "      <td>1.2214</td>\n",
       "      <td>2.7023</td>\n",
       "      <td>17</td>\n",
       "      <td>6683426742</td>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>957</th>\n",
       "      <td>957</td>\n",
       "      <td>1.1832</td>\n",
       "      <td>2.6891</td>\n",
       "      <td>58</td>\n",
       "      <td>6683426742</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4345</th>\n",
       "      <td>4345</td>\n",
       "      <td>1.1935</td>\n",
       "      <td>2.6550</td>\n",
       "      <td>11</td>\n",
       "      <td>6889790653</td>\n",
       "      <td>5</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4735</th>\n",
       "      <td>4735</td>\n",
       "      <td>1.1452</td>\n",
       "      <td>2.6074</td>\n",
       "      <td>49</td>\n",
       "      <td>6822359752</td>\n",
       "      <td>6</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5580</th>\n",
       "      <td>5580</td>\n",
       "      <td>1.0089</td>\n",
       "      <td>2.7287</td>\n",
       "      <td>19</td>\n",
       "      <td>1527921905</td>\n",
       "      <td>9</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "source": [
    "#星期天，实际weekday的值是6，星期一是0，星期二是1，星期三是2，星期四是3，星期五是4，星期六是5\n",
    "per = pd.Period('2025-02-28 18:00', 'h')\n",
    "per.weekday"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:31:54.275871Z",
     "start_time": "2025-02-28T02:31:54.270836Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "source": [
    "#观察数据，看下是否有空值，异常值\n",
    "data.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:32:49.619336Z",
     "start_time": "2025-02-28T02:32:49.606817Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             row_id             x             y      accuracy      place_id  \\\n",
       "count  1.771000e+04  17710.000000  17710.000000  17710.000000  1.771000e+04   \n",
       "mean   1.450569e+07      1.122538      2.632309     82.482101  5.129895e+09   \n",
       "std    8.353805e+06      0.077086      0.070144    113.613227  2.357399e+09   \n",
       "min    6.000000e+02      1.000100      2.500100      1.000000  1.012024e+09   \n",
       "25%    7.327816e+06      1.049200      2.573800     25.000000  3.312464e+09   \n",
       "50%    1.443071e+07      1.123300      2.642300     62.000000  5.261906e+09   \n",
       "75%    2.163463e+07      1.190500      2.687800     75.000000  6.766325e+09   \n",
       "max    2.911215e+07      1.249900      2.749900   1004.000000  9.980711e+09   \n",
       "\n",
       "                day          hour       weekday  \n",
       "count  17710.000000  17710.000000  17710.000000  \n",
       "mean       5.101863     11.485545      3.092377  \n",
       "std        2.709287      6.932195      1.680218  \n",
       "min        1.000000      0.000000      0.000000  \n",
       "25%        3.000000      6.000000      2.000000  \n",
       "50%        5.000000     12.000000      3.000000  \n",
       "75%        7.000000     17.000000      4.000000  \n",
       "max       10.000000     23.000000      6.000000  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>place_id</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.771000e+04</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>1.771000e+04</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "      <td>17710.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.450569e+07</td>\n",
       "      <td>1.122538</td>\n",
       "      <td>2.632309</td>\n",
       "      <td>82.482101</td>\n",
       "      <td>5.129895e+09</td>\n",
       "      <td>5.101863</td>\n",
       "      <td>11.485545</td>\n",
       "      <td>3.092377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.353805e+06</td>\n",
       "      <td>0.077086</td>\n",
       "      <td>0.070144</td>\n",
       "      <td>113.613227</td>\n",
       "      <td>2.357399e+09</td>\n",
       "      <td>2.709287</td>\n",
       "      <td>6.932195</td>\n",
       "      <td>1.680218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>6.000000e+02</td>\n",
       "      <td>1.000100</td>\n",
       "      <td>2.500100</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.012024e+09</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.327816e+06</td>\n",
       "      <td>1.049200</td>\n",
       "      <td>2.573800</td>\n",
       "      <td>25.000000</td>\n",
       "      <td>3.312464e+09</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.443071e+07</td>\n",
       "      <td>1.123300</td>\n",
       "      <td>2.642300</td>\n",
       "      <td>62.000000</td>\n",
       "      <td>5.261906e+09</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.163463e+07</td>\n",
       "      <td>1.190500</td>\n",
       "      <td>2.687800</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>6.766325e+09</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>17.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.911215e+07</td>\n",
       "      <td>1.249900</td>\n",
       "      <td>2.749900</td>\n",
       "      <td>1004.000000</td>\n",
       "      <td>9.980711e+09</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>23.000000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "source": [
    "# # 把签到数量少于n个目标位置删除，place_id是标签，即目标值\n",
    "place_count = data.groupby('place_id').count()\n",
    "place_count"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:33:39.478610Z",
     "start_time": "2025-02-28T02:33:39.472661Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "            row_id     x     y  accuracy   day  hour  weekday\n",
       "place_id                                                     \n",
       "1012023972       1     1     1         1     1     1        1\n",
       "1057182134       1     1     1         1     1     1        1\n",
       "1059958036       3     3     3         3     3     3        3\n",
       "1085266789       1     1     1         1     1     1        1\n",
       "1097200869    1044  1044  1044      1044  1044  1044     1044\n",
       "...            ...   ...   ...       ...   ...   ...      ...\n",
       "9904182060       1     1     1         1     1     1        1\n",
       "9915093501       1     1     1         1     1     1        1\n",
       "9946198589       1     1     1         1     1     1        1\n",
       "9950190890       1     1     1         1     1     1        1\n",
       "9980711012       5     5     5         5     5     5        5\n",
       "\n",
       "[805 rows x 7 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>place_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1012023972</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1057182134</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1059958036</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1085266789</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1097200869</th>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "      <td>1044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9904182060</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9915093501</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9946198589</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9950190890</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9980711012</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>805 rows × 7 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "source": [
    "place_count['x'].describe() #打卡地点总计805个，50%打卡小于2次"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:35:14.909946Z",
     "start_time": "2025-02-28T02:35:14.905600Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     805.000000\n",
       "mean       22.000000\n",
       "std        88.955632\n",
       "min         1.000000\n",
       "25%         1.000000\n",
       "50%         2.000000\n",
       "75%         5.000000\n",
       "max      1044.000000\n",
       "Name: x, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "cell_type": "code",
   "source": [
    "# # 把index变为0,1,2，3,4,5,6这种效果，从零开始排，原来的index是row_id\n",
    "#只选择去的人大于3的数据，认为1,2,3的是噪音，这个地方去的人很少，不用推荐给其他人\n",
    "tf = place_count[place_count.x > 3].reset_index()\n",
    "tf.shape  #剩余的签到地点只有239"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:38:46.253252Z",
     "start_time": "2025-02-28T02:38:46.249149Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(239, 8)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "source": [
    "# 根据设定的地点目标值，对原本的样本进行过滤\n",
    "#isin可以过滤某一列要在一组值\n",
    "data = data[data['place_id'].isin(tf.place_id)]\n",
    "data.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:39:19.477715Z",
     "start_time": "2025-02-28T02:39:19.473659Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(16918, 8)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "cell_type": "code",
   "source": [
    "# # 取出数据当中的特征值和目标值\n",
    "y = data['place_id']\n",
    "# 删除目标值，保留特征值，\n",
    "x = data.drop(['place_id'], axis=1)\n",
    "# 删除无用的特征值，row_id是索引,这就是噪音\n",
    "x = x.drop(['row_id'], axis=1)\n",
    "print(x.shape)\n",
    "print(x.columns)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:41:46.443664Z",
     "start_time": "2025-02-28T02:41:46.439059Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(16918, 6)\n",
      "Index(['x', 'y', 'accuracy', 'day', 'hour', 'weekday'], dtype='object')\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T02:41:56.804689Z",
     "start_time": "2025-02-28T02:41:56.799678Z"
    }
   },
   "cell_type": "code",
   "source": "y.shape",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(16918,)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 上面预处理完成"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "# li = load_iris()\n",
    "# x,y=li.data,li.target"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-17T02:41:30.779320300Z",
     "start_time": "2024-04-17T02:41:30.664384300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# 进行数据的分割训练集合测试集\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=1)\n",
    "\n",
    "# 特征工程（标准化）,下面3行注释，一开始我们不进行标准化，看下效果，目标值要不要标准化？\n",
    "std = StandardScaler()\n",
    "# #\n",
    "# # # 对测试集和训练集的特征值进行标准化,服务于knn fit\n",
    "x_train = std.fit_transform(x_train)\n",
    "# # transform返回的是copy，不在原有的输入对象中去修改\n",
    "# print(id(x_test))\n",
    "print(std.mean_)\n",
    "print(std.var_)\n",
    "x_test = std.transform(x_test)  #transfrom不再进行均值和方差的计算，是在原有的基础上去标准化\n",
    "print('-' * 50)\n",
    "# print(id(x_test))\n",
    "print(std.mean_)\n",
    "print(std.var_)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:47:22.691481Z",
     "start_time": "2025-02-28T02:47:22.682452Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.12295735  2.63237278 81.34938525  5.10064628 11.44293821  3.10135561]\n",
      "[5.98489138e-03 4.86857391e-03 1.19597480e+04 7.32837915e+00\n",
      " 4.83742660e+01 2.81838404e+00]\n",
      "--------------------------------------------------\n",
      "[ 1.12295735  2.63237278 81.34938525  5.10064628 11.44293821  3.10135561]\n",
      "[5.98489138e-03 4.86857391e-03 1.19597480e+04 7.32837915e+00\n",
      " 4.83742660e+01 2.81838404e+00]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "cell_type": "code",
   "source": [
    "x_train.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-28T02:47:37.170513Z",
     "start_time": "2025-02-28T02:47:37.167473Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12688, 6)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "source": [
    "# # 进行算法流程 # 超参数，可以通过设置n_neighbors=5，来调整结果好坏\n",
    "knn = KNeighborsClassifier(n_neighbors=3)\n",
    "\n",
    "# # fit， predict,score，训练，knn的fit是不训练的，只是把训练集的特征值和目标值放入到内存中\n",
    "knn.fit(x_train, y_train)\n",
    "# # #\n",
    "# # # 得出预测结果\n",
    "y_predict = knn.predict(x_test)\n",
    "# #\n",
    "print(\"预测的目标签到位置为：\", y_predict[0:10])\n",
    "# # #\n",
    "# # # # 得出准确率,是评估指标\n",
    "print(\"预测的准确率:\", knn.score(x_test, y_test))\n",
    "# print(y_predict)\n",
    "# y_test"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T02:50:31.661589Z",
     "start_time": "2025-02-28T02:50:31.499772Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的目标签到位置为： [5689129232 1097200869 6097504486 9632980559 6424972551 1097200869\n",
      " 3952821602 3533177779 1435128522 3312463746]\n",
      "预测的准确率: 0.46430260047281324\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "cell_type": "code",
   "source": "print(y_test[0:10])",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-10T03:06:07.589521Z",
     "start_time": "2025-01-10T03:06:07.586531Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16751286    1893548673\n",
      "12423167    1097200869\n",
      "7517023     6097504486\n",
      "4400015     9632980559\n",
      "26212472    6424972551\n",
      "7089828     4022692381\n",
      "10935607    2327054745\n",
      "25025511    3533177779\n",
      "27755137    1435128522\n",
      "19678934    3312463746\n",
      "Name: place_id, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 调超参的方法，网格搜索"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "#网格搜索时讲解\n",
    "# # 构造一些参数（超参）的值进行搜索\n",
    "param = {\"n_neighbors\": [3, 5, 10, 12, 15],'weights':['uniform', 'distance']}\n",
    "#\n",
    "# 进行网格搜索，cv=3是3折交叉验证，用其中2折训练，1折验证\n",
    "gc = GridSearchCV(knn, param_grid=param, cv=3)\n",
    "\n",
    "gc.fit(x_train, y_train)  #你给它的x_train，它又分为训练集，验证集\n",
    "\n",
    "# 预测准确率，为了给大家看看\n",
    "print(\"在测试集上准确率：\", gc.score(x_test, y_test))\n",
    "\n",
    "print(\"在交叉验证当中最好的结果：\", gc.best_score_) #最好的结果\n",
    "\n",
    "print(\"选择最好的模型是：\", gc.best_estimator_) #最好的模型,告诉你用了哪些参数\n",
    "\n",
    "print(\"每个超参数每次交叉验证的结果：\")\n",
    "gc.cv_results_"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-10T06:21:32.817350Z",
     "start_time": "2025-01-10T06:21:30.660842Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\41507\\AppData\\Roaming\\Python\\Python312\\site-packages\\sklearn\\model_selection\\_split.py:805: UserWarning: The least populated class in y has only 1 members, which is less than n_splits=3.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在测试集上准确率： 0.49763593380614657\n",
      "在交叉验证当中最好的结果： 0.4816362349278435\n",
      "选择最好的模型是： KNeighborsClassifier(n_neighbors=12, weights='distance')\n",
      "每个超参数每次交叉验证的结果：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'mean_fit_time': array([0.00553377, 0.00517241, 0.0057257 , 0.00479325, 0.00517448,\n",
       "        0.00516979, 0.00552996, 0.00569479, 0.00518004, 0.00527302]),\n",
       " 'std_fit_time': array([0.00044557, 0.00023863, 0.00040873, 0.0003015 , 0.00024559,\n",
       "        0.00023777, 0.00045045, 0.00043153, 0.00024013, 0.00038141]),\n",
       " 'mean_score_time': array([0.07551797, 0.03074328, 0.07429218, 0.03486315, 0.08469478,\n",
       "        0.04764803, 0.08502666, 0.0509603 , 0.09069777, 0.05944928]),\n",
       " 'std_score_time': array([0.00501248, 0.00047674, 0.0004511 , 0.00069377, 0.00130438,\n",
       "        0.00150017, 0.00090986, 0.00026141, 0.00206   , 0.00233067]),\n",
       " 'param_n_neighbors': masked_array(data=[3, 3, 5, 5, 10, 10, 12, 12, 15, 15],\n",
       "              mask=[False, False, False, False, False, False, False, False,\n",
       "                    False, False],\n",
       "        fill_value=999999),\n",
       " 'param_weights': masked_array(data=['uniform', 'distance', 'uniform', 'distance',\n",
       "                    'uniform', 'distance', 'uniform', 'distance',\n",
       "                    'uniform', 'distance'],\n",
       "              mask=[False, False, False, False, False, False, False, False,\n",
       "                    False, False],\n",
       "        fill_value='?',\n",
       "             dtype=object),\n",
       " 'params': [{'n_neighbors': 3, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 3, 'weights': 'distance'},\n",
       "  {'n_neighbors': 5, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 5, 'weights': 'distance'},\n",
       "  {'n_neighbors': 10, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 10, 'weights': 'distance'},\n",
       "  {'n_neighbors': 12, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 12, 'weights': 'distance'},\n",
       "  {'n_neighbors': 15, 'weights': 'uniform'},\n",
       "  {'n_neighbors': 15, 'weights': 'distance'}],\n",
       " 'split0_test_score': array([0.44468085, 0.4534279 , 0.4607565 , 0.47399527, 0.46170213,\n",
       "        0.48014184, 0.45650118, 0.48108747, 0.45508274, 0.47895981]),\n",
       " 'split1_test_score': array([0.43390873, 0.4542445 , 0.45660913, 0.47528967, 0.45542681,\n",
       "        0.48238354, 0.45329865, 0.48049184, 0.44809648, 0.47623552]),\n",
       " 'split2_test_score': array([0.43982029, 0.4561362 , 0.45684559, 0.47221565, 0.4618113 ,\n",
       "        0.48191062, 0.45897375, 0.48332939, 0.46062899, 0.48049184]),\n",
       " 'mean_test_score': array([0.43946996, 0.45460287, 0.45807041, 0.47383353, 0.45964675,\n",
       "        0.48147867, 0.45625786, 0.48163623, 0.45460274, 0.47856239]),\n",
       " 'std_test_score': array([0.00440467, 0.00113433, 0.00190181, 0.00126016, 0.00298428,\n",
       "        0.00096479, 0.00232323, 0.00122169, 0.00512762, 0.00176021]),\n",
       " 'rank_test_score': array([10,  8,  6,  4,  5,  2,  7,  1,  9,  3])}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "朴素贝叶斯进行文本分类\n",
    ":return: None\n",
    "\"\"\"\n",
    "news = fetch_20newsgroups(subset='all', data_home='data')\n",
    "\n",
    "print(len(news.data))  #样本数，包含的特征\n",
    "print('-'*50)\n",
    "print(news.data[0]) #第一个样本 特征\n",
    "print('-'*50)\n",
    "print(news.target[0:5]) #标签\n",
    "print(np.unique(news.target)) #标签的类别\n",
    "print(news.target_names) #标签的名字"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T06:36:58.498607Z",
     "start_time": "2025-02-28T06:36:58.268161Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18846\n",
      "--------------------------------------------------\n",
      "From: Mamatha Devineni Ratnam <mr47+@andrew.cmu.edu>\n",
      "Subject: Pens fans reactions\n",
      "Organization: Post Office, Carnegie Mellon, Pittsburgh, PA\n",
      "Lines: 12\n",
      "NNTP-Posting-Host: po4.andrew.cmu.edu\n",
      "\n",
      "\n",
      "\n",
      "I am sure some bashers of Pens fans are pretty confused about the lack\n",
      "of any kind of posts about the recent Pens massacre of the Devils. Actually,\n",
      "I am  bit puzzled too and a bit relieved. However, I am going to put an end\n",
      "to non-PIttsburghers' relief with a bit of praise for the Pens. Man, they\n",
      "are killing those Devils worse than I thought. Jagr just showed you why\n",
      "he is much better than his regular season stats. He is also a lot\n",
      "fo fun to watch in the playoffs. Bowman should let JAgr have a lot of\n",
      "fun in the next couple of games since the Pens are going to beat the pulp out of Jersey anyway. I was very disappointed not to see the Islanders lose the final\n",
      "regular season game.          PENS RULE!!!\n",
      "\n",
      "\n",
      "--------------------------------------------------\n",
      "[10  3 17  3  4]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]\n",
      "['alt.atheism', 'comp.graphics', 'comp.os.ms-windows.misc', 'comp.sys.ibm.pc.hardware', 'comp.sys.mac.hardware', 'comp.windows.x', 'misc.forsale', 'rec.autos', 'rec.motorcycles', 'rec.sport.baseball', 'rec.sport.hockey', 'sci.crypt', 'sci.electronics', 'sci.med', 'sci.space', 'soc.religion.christian', 'talk.politics.guns', 'talk.politics.mideast', 'talk.politics.misc', 'talk.religion.misc']\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "source": [
    "print('-'*50)\n",
    "# 进行数据分割\n",
    "x_train, x_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25, random_state=1)\n",
    "\n",
    "# 对数据集进行特征抽取\n",
    "tf = TfidfVectorizer()\n",
    "print(len(x_train))\n",
    "# 以训练集当中的词的列表进行每篇文章重要性统计['a','b','c','d']\n",
    "x_train = tf.fit_transform(x_train)\n",
    "#针对特征内容，可以自行打印，下面的打印可以得到特征数目，总计有15万特征\n",
    "print(len(tf.get_feature_names_out()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T06:38:42.716307Z",
     "start_time": "2025-02-28T06:38:40.794014Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "14134\n",
      "153196\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T06:37:55.613139Z",
     "start_time": "2025-02-28T06:37:55.608628Z"
    }
   },
   "cell_type": "code",
   "source": "x_train.shape",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(14134, 153196)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "source": "print(tf.get_feature_names_out()[50000])",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T06:39:51.277803Z",
     "start_time": "2025-02-28T06:39:51.204160Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conspiratorial\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "cell_type": "code",
   "source": "print(tf.get_feature_names_out()[0:20])",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T06:39:16.536715Z",
     "start_time": "2025-02-28T06:39:16.473615Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['00' '000' '0000' '00000' '0000000004' '0000000005' '0000000667'\n",
      " '0000001200' '000003' '000005102000' '00000510200001' '000007' '00000f'\n",
      " '000020' '000021' '000042' '000050' '000062david42' '000094' '0000ahc']\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-10T07:00:11.489654Z",
     "start_time": "2025-01-10T07:00:11.427281Z"
    }
   },
   "cell_type": "code",
   "source": "print(tf.get_feature_names_out()[100000:100000+10])",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['murky' 'murmurs' 'murnane' 'murph' 'murphey' 'murphy' 'murr11' 'murray'\n",
      " 'murray_craven' 'murrayfield']\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T06:41:55.236196Z",
     "start_time": "2025-02-28T06:41:53.100381Z"
    }
   },
   "cell_type": "code",
   "source": "x_train.toarray()[0]",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.114873637322706"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "cell_type": "code",
   "source": [
    "import time\n",
    "# 进行朴素贝叶斯算法的预测,alpha是拉普拉斯平滑系数，分子和分母加上一个系数，分母加alpha*特征词数目\n",
    "mlt = MultinomialNB(alpha=1.0)\n",
    "\n",
    "# print(x_train.toarray())\n",
    "# 训练\n",
    "start=time.time()\n",
    "mlt.fit(x_train, y_train) #训练模型\n",
    "end=time.time()\n",
    "end-start #统计训练时间"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T06:42:54.723982Z",
     "start_time": "2025-02-28T06:42:54.656929Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.06404662132263184"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 44
  },
  {
   "cell_type": "code",
   "source": [
    "x_transform_test = tf.transform(x_test)  #特征数目不发生改变\n",
    "print(len(tf.get_feature_names_out())) #查看特征数目"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-28T06:45:03.866075Z",
     "start_time": "2025-02-28T06:45:03.332539Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153196\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T06:45:07.802609Z",
     "start_time": "2025-02-28T06:45:07.799204Z"
    }
   },
   "cell_type": "code",
   "source": "x_transform_test.shape",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4712, 153196)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 46
  },
  {
   "cell_type": "code",
   "source": [
    "start=time.time()\n",
    "y_predict = mlt.predict(x_transform_test)\n",
    "\n",
    "print(\"预测的前面10篇文章类别为：\", y_predict[0:10])\n",
    "\n",
    "# 得出准确率,这个是很难提高准确率，为什么呢？\n",
    "print(\"准确率为：\", mlt.score(x_transform_test, y_test))\n",
    "end=time.time()\n",
    "end-start #预测时间"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T06:47:17.863979Z",
     "start_time": "2025-02-28T06:47:17.828133Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的前面10篇文章类别为： [16 19 18  1  9 15  1  2 16 13]\n",
      "准确率为： 0.8518675721561969\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.032341718673706055"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 47
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "4712"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#预测的文章数目\n",
    "len(y_predict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-04-17T03:40:11.018365400Z",
     "start_time": "2024-04-17T03:40:10.999378200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# 目前这个场景我们不需要召回率，support是真实的为那个类别的有多少个样本\n",
    "print(classification_report(y_test, y_predict,\n",
    "      target_names=news.target_names))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T06:48:40.602175Z",
     "start_time": "2025-02-28T06:48:40.594673Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          precision    recall  f1-score   support\n",
      "\n",
      "             alt.atheism       0.91      0.77      0.83       199\n",
      "           comp.graphics       0.83      0.79      0.81       242\n",
      " comp.os.ms-windows.misc       0.89      0.83      0.86       263\n",
      "comp.sys.ibm.pc.hardware       0.80      0.83      0.81       262\n",
      "   comp.sys.mac.hardware       0.90      0.88      0.89       234\n",
      "          comp.windows.x       0.92      0.85      0.88       230\n",
      "            misc.forsale       0.96      0.67      0.79       257\n",
      "               rec.autos       0.90      0.87      0.88       265\n",
      "         rec.motorcycles       0.90      0.95      0.92       251\n",
      "      rec.sport.baseball       0.89      0.96      0.93       226\n",
      "        rec.sport.hockey       0.95      0.98      0.96       262\n",
      "               sci.crypt       0.76      0.97      0.85       257\n",
      "         sci.electronics       0.84      0.80      0.82       229\n",
      "                 sci.med       0.97      0.86      0.91       249\n",
      "               sci.space       0.92      0.96      0.94       256\n",
      "  soc.religion.christian       0.55      0.98      0.70       243\n",
      "      talk.politics.guns       0.76      0.96      0.85       234\n",
      "   talk.politics.mideast       0.93      0.99      0.96       224\n",
      "      talk.politics.misc       0.98      0.56      0.72       197\n",
      "      talk.religion.misc       0.97      0.26      0.41       132\n",
      "\n",
      "                accuracy                           0.85      4712\n",
      "               macro avg       0.88      0.84      0.84      4712\n",
      "            weighted avg       0.87      0.85      0.85      4712\n",
      "\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "(4712,)"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.shape #测试集中有多少 样本"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-11T08:41:49.409254600Z",
     "start_time": "2024-07-11T08:41:49.394684800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "y_test1 = np.where(y_test == 0, 1, 0)\n",
    "print(y_test1.sum()) #label为0的样本数"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-28T06:50:21.700485Z",
     "start_time": "2025-02-28T06:50:21.696717Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "199\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "cell_type": "code",
   "source": [
    "y_predict1 = np.where(y_predict == 0, 1, 0)\n",
    "print(y_predict1.sum())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-10T07:28:12.271834Z",
     "start_time": "2025-01-10T07:28:12.269240Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "168\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-10T07:31:50.300865Z",
     "start_time": "2025-01-10T07:31:50.298207Z"
    }
   },
   "cell_type": "code",
   "source": "print(y_test1[0:20])",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0]\n"
     ]
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-10T07:32:00.820878Z",
     "start_time": "2025-01-10T07:32:00.817985Z"
    }
   },
   "cell_type": "code",
   "source": "y_predict1[0:20]",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 51
  },
  {
   "cell_type": "code",
   "source": [
    "#168个预测为正例中，有153预测正确了\n",
    "(y_test1*y_predict1).sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-10T07:31:02.616559Z",
     "start_time": "2025-01-10T07:31:02.611947Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "153"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 49
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "data": {
      "text/plain": "0.9107142857142857"
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "153/168"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-11T08:47:47.240582700Z",
     "start_time": "2024-07-11T08:47:47.224194200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "data": {
      "text/plain": "0.7688442211055276"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "153/199"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-11T08:47:58.745023500Z",
     "start_time": "2024-07-11T08:47:58.679632Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "(19, 0)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(y_test),min(y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-18T01:45:20.411727800Z",
     "start_time": "2024-04-18T01:45:20.386731300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# 把0-19总计20个分类，变为0和1\n",
    "# 5是可以改为0到19的\n",
    "y_test1 = np.where(y_test == 5, 1, 0)\n",
    "print(y_test1.sum()) #label为5的样本数\n",
    "y_predict1 = np.where(y_predict == 5, 1, 0)\n",
    "print(y_predict1.sum())\n",
    "# roc_auc_score的y_test只能是二分类,针对多分类如何计算AUC\n",
    "print(\"AUC指标：\", roc_auc_score(y_test1, y_predict1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T06:56:38.156701Z",
     "start_time": "2025-02-28T06:56:38.152718Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "230\n",
      "214\n",
      "AUC指标： 0.924078924393225\n"
     ]
    }
   ],
   "execution_count": 51
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "(array([0, 0, 0, ..., 0, 0, 0]), array([0, 0, 0, ..., 0, 0, 0]))"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test1,y_predict1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-18T01:49:37.220158600Z",
     "start_time": "2024-04-18T01:49:37.200170Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "196\n",
      "34\n",
      "4464\n"
     ]
    }
   ],
   "source": [
    "#算多分类的精确率，召回率，F1-score\n",
    "FP=np.where((np.array(y_test1)-np.array(y_predict1))==-1,1,0).sum()   #FP是18\n",
    "TP=y_predict1.sum()-FP #TP是196\n",
    "print(TP)\n",
    "FN=np.where((np.array(y_test1)-np.array(y_predict1))==1,1,0).sum() #FN是34\n",
    "print(FN)#FN是1\n",
    "TN=np.where(y_test1==0,1,0).sum()-FP  #4464\n",
    "print(TN)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-04-18T02:02:14.916876200Z",
     "start_time": "2024-04-18T02:02:14.879886600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "0.9158878504672897"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TP/(TP+FP) #精确率"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-18T02:02:17.581304500Z",
     "start_time": "2024-04-18T02:02:17.543326400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "0.8521739130434782"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TP/(TP+FN)  #召回率"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-18T02:03:10.184400300Z",
     "start_time": "2024-04-18T02:03:10.130432500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "0.8828828828828829"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#F1-score\n",
    "2*TP/(2*TP+FP+FN)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-18T02:03:31.272589Z",
     "start_time": "2024-04-18T02:03:31.224618200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "del news\n",
    "del x_train\n",
    "del x_test\n",
    "del y_test\n",
    "del y_predict\n",
    "del tf"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3 决策树"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.feature_extraction import DictVectorizer\n",
    "from sklearn.tree import DecisionTreeClassifier, export_graphviz\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.311448Z",
     "start_time": "2025-01-11T02:24:45.410415Z"
    }
   },
   "outputs": [],
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "source": [
    "np.log2(1/32)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.315360Z",
     "start_time": "2025-01-11T02:24:50.311448Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-5.0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.318020Z",
     "start_time": "2025-01-11T02:24:50.315360Z"
    }
   },
   "cell_type": "code",
   "source": "np.log2(1)",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.320306Z",
     "start_time": "2025-01-11T02:24:50.318020Z"
    }
   },
   "cell_type": "code",
   "source": "# bit 位",
   "outputs": [],
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "source": [
    "1 / 2 * np.log2(1 /2) + 1 / 2 * np.log2(1 /2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.323351Z",
     "start_time": "2025-01-11T02:24:50.320306Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "source": "0.01 * np.log2(0.01) + 0.99 * np.log2(0.99)",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T07:46:04.577934Z",
     "start_time": "2025-02-28T07:46:04.573209Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.08079313589591118"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 52
  },
  {
   "cell_type": "code",
   "source": [
    "0.01 * np.log2(0.01) + 0.99 * np.log2(0.99)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.329450Z",
     "start_time": "2025-01-11T02:24:50.326446Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.08079313589591118"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "决策树对泰坦尼克号进行预测生死\n",
    ":return: None\n",
    "\"\"\"\n",
    "# 获取数据\n",
    "titan = pd.read_csv(\"./data/titanic.txt\")\n",
    "titan.info()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T07:58:39.779012Z",
     "start_time": "2025-02-28T07:58:39.754797Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1313 entries, 0 to 1312\n",
      "Data columns (total 11 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   row.names  1313 non-null   int64  \n",
      " 1   pclass     1313 non-null   object \n",
      " 2   survived   1313 non-null   int64  \n",
      " 3   name       1313 non-null   object \n",
      " 4   age        633 non-null    float64\n",
      " 5   embarked   821 non-null    object \n",
      " 6   home.dest  754 non-null    object \n",
      " 7   room       77 non-null     object \n",
      " 8   ticket     69 non-null     object \n",
      " 9   boat       347 non-null    object \n",
      " 10  sex        1313 non-null   object \n",
      "dtypes: float64(1), int64(2), object(8)\n",
      "memory usage: 113.0+ KB\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.352291Z",
     "start_time": "2025-01-11T02:24:50.343367Z"
    }
   },
   "cell_type": "code",
   "source": "titan",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      row.names pclass  survived  \\\n",
       "0             1    1st         1   \n",
       "1             2    1st         0   \n",
       "2             3    1st         0   \n",
       "3             4    1st         0   \n",
       "4             5    1st         1   \n",
       "...         ...    ...       ...   \n",
       "1308       1309    3rd         0   \n",
       "1309       1310    3rd         0   \n",
       "1310       1311    3rd         0   \n",
       "1311       1312    3rd         0   \n",
       "1312       1313    3rd         0   \n",
       "\n",
       "                                                 name      age     embarked  \\\n",
       "0                        Allen, Miss Elisabeth Walton  29.0000  Southampton   \n",
       "1                         Allison, Miss Helen Loraine   2.0000  Southampton   \n",
       "2                 Allison, Mr Hudson Joshua Creighton  30.0000  Southampton   \n",
       "3     Allison, Mrs Hudson J.C. (Bessie Waldo Daniels)  25.0000  Southampton   \n",
       "4                       Allison, Master Hudson Trevor   0.9167  Southampton   \n",
       "...                                               ...      ...          ...   \n",
       "1308                               Zakarian, Mr Artun      NaN          NaN   \n",
       "1309                           Zakarian, Mr Maprieder      NaN          NaN   \n",
       "1310                                  Zenn, Mr Philip      NaN          NaN   \n",
       "1311                                    Zievens, Rene      NaN          NaN   \n",
       "1312                                   Zimmerman, Leo      NaN          NaN   \n",
       "\n",
       "                            home.dest room      ticket   boat     sex  \n",
       "0                        St Louis, MO  B-5  24160 L221      2  female  \n",
       "1     Montreal, PQ / Chesterville, ON  C26         NaN    NaN  female  \n",
       "2     Montreal, PQ / Chesterville, ON  C26         NaN  (135)    male  \n",
       "3     Montreal, PQ / Chesterville, ON  C26         NaN    NaN  female  \n",
       "4     Montreal, PQ / Chesterville, ON  C22         NaN     11    male  \n",
       "...                               ...  ...         ...    ...     ...  \n",
       "1308                              NaN  NaN         NaN    NaN    male  \n",
       "1309                              NaN  NaN         NaN    NaN    male  \n",
       "1310                              NaN  NaN         NaN    NaN    male  \n",
       "1311                              NaN  NaN         NaN    NaN  female  \n",
       "1312                              NaN  NaN         NaN    NaN    male  \n",
       "\n",
       "[1313 rows x 11 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row.names</th>\n",
       "      <th>pclass</th>\n",
       "      <th>survived</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>embarked</th>\n",
       "      <th>home.dest</th>\n",
       "      <th>room</th>\n",
       "      <th>ticket</th>\n",
       "      <th>boat</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1st</td>\n",
       "      <td>1</td>\n",
       "      <td>Allen, Miss Elisabeth Walton</td>\n",
       "      <td>29.0000</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>St Louis, MO</td>\n",
       "      <td>B-5</td>\n",
       "      <td>24160 L221</td>\n",
       "      <td>2</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1st</td>\n",
       "      <td>0</td>\n",
       "      <td>Allison, Miss Helen Loraine</td>\n",
       "      <td>2.0000</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>Montreal, PQ / Chesterville, ON</td>\n",
       "      <td>C26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1st</td>\n",
       "      <td>0</td>\n",
       "      <td>Allison, Mr Hudson Joshua Creighton</td>\n",
       "      <td>30.0000</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>Montreal, PQ / Chesterville, ON</td>\n",
       "      <td>C26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>(135)</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1st</td>\n",
       "      <td>0</td>\n",
       "      <td>Allison, Mrs Hudson J.C. (Bessie Waldo Daniels)</td>\n",
       "      <td>25.0000</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>Montreal, PQ / Chesterville, ON</td>\n",
       "      <td>C26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1st</td>\n",
       "      <td>1</td>\n",
       "      <td>Allison, Master Hudson Trevor</td>\n",
       "      <td>0.9167</td>\n",
       "      <td>Southampton</td>\n",
       "      <td>Montreal, PQ / Chesterville, ON</td>\n",
       "      <td>C22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1308</th>\n",
       "      <td>1309</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zakarian, Mr Artun</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1309</th>\n",
       "      <td>1310</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zakarian, Mr Maprieder</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1310</th>\n",
       "      <td>1311</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zenn, Mr Philip</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1311</th>\n",
       "      <td>1312</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zievens, Rene</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1312</th>\n",
       "      <td>1313</td>\n",
       "      <td>3rd</td>\n",
       "      <td>0</td>\n",
       "      <td>Zimmerman, Leo</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1313 rows × 11 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "source": [
    "# 处理数据，找出特征值和目标值\n",
    "x = titan.loc[:,['pclass', 'age', 'sex']]\n",
    "\n",
    "y = titan.loc[:,'survived']\n",
    "print(x.info())  # 用来判断是否有空值\n",
    "x.describe(include='all')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T08:01:06.975962Z",
     "start_time": "2025-02-28T08:01:06.963898Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1313 entries, 0 to 1312\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   pclass  1313 non-null   object \n",
      " 1   age     633 non-null    float64\n",
      " 2   sex     1313 non-null   object \n",
      "dtypes: float64(1), object(2)\n",
      "memory usage: 30.9+ KB\n",
      "None\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "       pclass         age   sex\n",
       "count    1313  633.000000  1313\n",
       "unique      3         NaN     2\n",
       "top       3rd         NaN  male\n",
       "freq      711         NaN   850\n",
       "mean      NaN   31.194181   NaN\n",
       "std       NaN   14.747525   NaN\n",
       "min       NaN    0.166700   NaN\n",
       "25%       NaN   21.000000   NaN\n",
       "50%       NaN   30.000000   NaN\n",
       "75%       NaN   41.000000   NaN\n",
       "max       NaN   71.000000   NaN"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pclass</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1313</td>\n",
       "      <td>633.000000</td>\n",
       "      <td>1313</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>3rd</td>\n",
       "      <td>NaN</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>711</td>\n",
       "      <td>NaN</td>\n",
       "      <td>850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>NaN</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>NaN</td>\n",
       "      <td>14.747525</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.166700</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>NaN</td>\n",
       "      <td>21.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>NaN</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>NaN</td>\n",
       "      <td>41.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>NaN</td>\n",
       "      <td>71.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 54
  },
  {
   "cell_type": "code",
   "source": [
    "x.loc[:,'age'].max()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.366201Z",
     "start_time": "2025-01-11T02:24:50.363206Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "71.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "source": [
    "# 一定要进行缺失值处理,填为均值\n",
    "mean=x['age'].mean()\n",
    "print(mean)\n",
    "x.loc[:,'age']=x.loc[:,'age'].fillna(mean)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T08:04:48.415817Z",
     "start_time": "2025-02-28T08:04:48.411530Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31.19418104265403\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "cell_type": "code",
   "source": [
    "x.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-28T08:04:51.734466Z",
     "start_time": "2025-02-28T08:04:51.729829Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1313 entries, 0 to 1312\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   pclass  1313 non-null   object \n",
      " 1   age     1313 non-null   float64\n",
      " 2   sex     1313 non-null   object \n",
      "dtypes: float64(1), object(2)\n",
      "memory usage: 30.9+ KB\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "cell_type": "code",
   "source": [
    "\n",
    "\n",
    "# 分割数据集到训练集合测试集\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=4)\n",
    "print(x_train.head())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T08:05:04.592182Z",
     "start_time": "2025-02-28T08:05:04.583687Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    pclass        age     sex\n",
      "598    2nd  30.000000    male\n",
      "246    1st  62.000000    male\n",
      "905    3rd  31.194181  female\n",
      "300    1st  31.194181  female\n",
      "509    2nd  64.000000    male\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "cell_type": "code",
   "source": [
    "type(x_train)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.380587Z",
     "start_time": "2025-01-11T02:24:50.377578Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "source": [
    "sum(y_train)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.383738Z",
     "start_time": "2025-01-11T02:24:50.380587Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "334"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T08:05:39.890227Z",
     "start_time": "2025-02-28T08:05:39.887226Z"
    }
   },
   "cell_type": "code",
   "source": "x_train.shape",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(984, 3)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 58
  },
  {
   "cell_type": "code",
   "source": [
    "#性别是女性的数量\n",
    "x_train[x_train['sex'] == 'female'].count()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.387928Z",
     "start_time": "2025-01-11T02:24:50.383738Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pclass    341\n",
       "age       341\n",
       "sex       341\n",
       "dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "source": [
    "y_train"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.391681Z",
     "start_time": "2025-01-11T02:24:50.387928Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "598     0\n",
       "246     0\n",
       "905     0\n",
       "300     0\n",
       "509     0\n",
       "       ..\n",
       "360     0\n",
       "709     0\n",
       "439     0\n",
       "174     0\n",
       "1146    0\n",
       "Name: survived, Length: 984, dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "source": [
    "#女性中存活的情况对比\n",
    "z=x_train.copy() #z是为了把特征和目标存储到一起\n",
    "z['survived'] = y_train #把目标值存储到z中\n",
    "z"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-28T08:06:24.279428Z",
     "start_time": "2025-02-28T08:06:24.274545Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     pclass        age     sex  survived\n",
       "598     2nd  30.000000    male         0\n",
       "246     1st  62.000000    male         0\n",
       "905     3rd  31.194181  female         0\n",
       "300     1st  31.194181  female         0\n",
       "509     2nd  64.000000    male         0\n",
       "...     ...        ...     ...       ...\n",
       "360     2nd  31.194181    male         0\n",
       "709     3rd  28.000000    male         0\n",
       "439     2nd  34.000000    male         0\n",
       "174     1st  46.000000    male         0\n",
       "1146    3rd  31.194181    male         0\n",
       "\n",
       "[984 rows x 4 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pclass</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>survived</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>598</th>\n",
       "      <td>2nd</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>246</th>\n",
       "      <td>1st</td>\n",
       "      <td>62.000000</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>905</th>\n",
       "      <td>3rd</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>female</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300</th>\n",
       "      <td>1st</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>female</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>509</th>\n",
       "      <td>2nd</td>\n",
       "      <td>64.000000</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>360</th>\n",
       "      <td>2nd</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>709</th>\n",
       "      <td>3rd</td>\n",
       "      <td>28.000000</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>439</th>\n",
       "      <td>2nd</td>\n",
       "      <td>34.000000</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>174</th>\n",
       "      <td>1st</td>\n",
       "      <td>46.000000</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1146</th>\n",
       "      <td>3rd</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>984 rows × 4 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T08:06:41.632114Z",
     "start_time": "2025-02-28T08:06:41.620001Z"
    }
   },
   "cell_type": "code",
   "source": "z[z['sex'] == 'female']['survived'].value_counts()  #男性中存活的情况,value_counts()类似对survived分组，再统计每组的数量",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "survived\n",
       "1    230\n",
       "0    111\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-28T08:07:51.519405Z",
     "start_time": "2025-02-28T08:07:51.514899Z"
    }
   },
   "cell_type": "code",
   "source": "z[z['sex'] == 'male']['survived'].value_counts() ",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "survived\n",
       "0    539\n",
       "1    104\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  },
  {
   "cell_type": "code",
   "source": [
    "y_train.value_counts() #没存活的是650，存活的是334"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.403431Z",
     "start_time": "2025-01-11T02:24:50.399976Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "survived\n",
       "0    650\n",
       "1    334\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "source": [
    "x_train.loc[:,'sex'].value_counts()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.408481Z",
     "start_time": "2025-01-11T02:24:50.403431Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sex\n",
       "male      643\n",
       "female    341\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "source": [
    "230/(230+111)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.411594Z",
     "start_time": "2025-01-11T02:24:50.408481Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6744868035190615"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "cell_type": "code",
   "source": [
    "#查看未存活的人的数量\n",
    "x_train"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.415929Z",
     "start_time": "2025-01-11T02:24:50.411594Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     pclass        age     sex\n",
       "598     2nd  30.000000    male\n",
       "246     1st  62.000000    male\n",
       "905     3rd  31.194181  female\n",
       "300     1st  31.194181  female\n",
       "509     2nd  64.000000    male\n",
       "...     ...        ...     ...\n",
       "360     2nd  31.194181    male\n",
       "709     3rd  28.000000    male\n",
       "439     2nd  34.000000    male\n",
       "174     1st  46.000000    male\n",
       "1146    3rd  31.194181    male\n",
       "\n",
       "[984 rows x 3 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pclass</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>598</th>\n",
       "      <td>2nd</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>246</th>\n",
       "      <td>1st</td>\n",
       "      <td>62.000000</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>905</th>\n",
       "      <td>3rd</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>300</th>\n",
       "      <td>1st</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>509</th>\n",
       "      <td>2nd</td>\n",
       "      <td>64.000000</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>360</th>\n",
       "      <td>2nd</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>709</th>\n",
       "      <td>3rd</td>\n",
       "      <td>28.000000</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>439</th>\n",
       "      <td>2nd</td>\n",
       "      <td>34.000000</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>174</th>\n",
       "      <td>1st</td>\n",
       "      <td>46.000000</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1146</th>\n",
       "      <td>3rd</td>\n",
       "      <td>31.194181</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>984 rows × 3 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "source": [
    "x_train.to_dict(orient=\"records\") #把df变为字典，样本变为一个一个的字典，字典中列名变为键"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.445794Z",
     "start_time": "2025-01-11T02:24:50.415929Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 62.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 64.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 24.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 23.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 44.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 37.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 6.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 41.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 25.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 24.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 57.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 36.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 51.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 43.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 16.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 13.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 45.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 22.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 16.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 47.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 20.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 57.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 58.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 61.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 38.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 27.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 35.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 58.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 40.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 32.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 54.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 38.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 39.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 52.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 60.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 11.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 45.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 18.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 28.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 19.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 65.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 55.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 36.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 45.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 20.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 24.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 15.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 45.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 35.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 22.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 48.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 48.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 8.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 27.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 45.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 18.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 5.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 39.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 33.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 9.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 23.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 17.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 58.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 12.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 28.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 18.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 36.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 32.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 37.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 44.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 17.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 32.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 52.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 40.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 27.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 22.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 40.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 33.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 18.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 37.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 15.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 21.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 30.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 44.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 39.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 13.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 12.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 2.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 27.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 64.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 40.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 42.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 3.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 5.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 22.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 33.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 45.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 20.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 54.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 11.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 39.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 32.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 45.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 10.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 36.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 18.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 18.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 22.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 44.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 38.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 27.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 36.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 20.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 6.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 16.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 0.3333, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 21.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 17.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 55.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 23.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 40.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 48.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 33.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 49.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 47.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 48.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 45.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 43.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 52.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 39.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 9.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 34.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 23.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 27.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 59.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 28.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 41.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 18.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 27.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 33.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 39.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 8.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 43.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 44.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 36.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 32.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 61.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 29.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 29.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 52.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 42.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 2.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 17.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 15.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 49.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 45.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 20.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 3.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 37.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 57.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 24.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 54.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 3.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 25.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 17.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 45.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 48.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 0.1667, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 8.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 35.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 34.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 33.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 7.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 27.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 50.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 39.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 50.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 46.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 35.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 24.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 17.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 27.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 33.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 60.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 58.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 33.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 38.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 54.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 63.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 0.9167, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 11.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 19.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 43.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 2.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 45.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 26.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 56.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 19.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 4.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 45.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 22.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 64.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 32.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 17.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 55.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 43.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 17.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 51.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 35.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 24.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 21.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 27.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 53.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 40.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 41.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 69.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 29.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 22.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 48.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 47.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 32.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 53.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 45.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 67.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 2.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 32.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 2.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 35.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 6.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 50.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 36.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 26.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 56.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 19.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 48.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 1.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 27.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 16.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 1.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 30.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 16.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 4.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 16.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 40.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 58.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 60.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 32.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 39.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 16.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 27.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 42.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 38.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 32.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 20.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 13.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 32.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 45.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 29.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 20.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 40.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 32.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 32.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 3.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 35.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 37.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 47.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 21.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 22.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 22.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 48.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 25.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 45.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 9.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 59.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 19.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 19.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 42.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 42.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 29.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 38.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 33.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 19.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 41.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 34.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 33.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 5.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 18.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 42.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 14.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 29.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 30.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 48.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 18.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 35.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 20.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 42.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 17.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 71.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 29.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 4.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 30.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 22.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 19.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 30.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 40.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 25.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 6.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 50.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 71.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 9.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 53.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 71.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 46.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 49.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 1.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 46.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 37.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 34.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 22.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 19.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 46.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 58.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 22.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 45.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 35.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 41.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 46.0, 'sex': 'female'},\n",
       " {'pclass': '1st', 'age': 36.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 24.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 44.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 57.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 9.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 23.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 24.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 47.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 50.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 50.0, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 38.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 22.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 18.0, 'sex': 'female'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'female'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 31.19418104265403, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 28.0, 'sex': 'male'},\n",
       " {'pclass': '2nd', 'age': 34.0, 'sex': 'male'},\n",
       " {'pclass': '1st', 'age': 46.0, 'sex': 'male'},\n",
       " {'pclass': '3rd', 'age': 31.19418104265403, 'sex': 'male'}]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "source": [
    "# 进行处理（特征工程）特征-》类别-》one_hot编码\n",
    "dict = DictVectorizer(sparse=False)\n",
    "\n",
    "# 这一步是对字典进行特征抽取,to_dict可以把df变为字典，records代表列名变为键\n",
    "x_train = dict.fit_transform(x_train.to_dict(orient=\"records\"))\n",
    "print(type(x_train))\n",
    "print(dict.get_feature_names_out())\n",
    "print('-' * 50)\n",
    "x_test = dict.transform(x_test.to_dict(orient=\"records\"))\n",
    "print(x_train)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T08:11:42.488402Z",
     "start_time": "2025-02-28T08:11:42.472691Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "['age' 'pclass=1st' 'pclass=2nd' 'pclass=3rd' 'sex=female' 'sex=male']\n",
      "--------------------------------------------------\n",
      "[[30.          0.          1.          0.          0.          1.        ]\n",
      " [62.          1.          0.          0.          0.          1.        ]\n",
      " [31.19418104  0.          0.          1.          1.          0.        ]\n",
      " ...\n",
      " [34.          0.          1.          0.          0.          1.        ]\n",
      " [46.          1.          0.          0.          0.          1.        ]\n",
      " [31.19418104  0.          0.          1.          0.          1.        ]]\n"
     ]
    }
   ],
   "execution_count": 63
  },
  {
   "cell_type": "code",
   "source": [
    "# 用决策树进行预测，修改max_depth试试,修改criterion为entropy\n",
    "#树过于复杂，就会产生过拟合\n",
    "dec = DecisionTreeClassifier()\n",
    "\n",
    "#训练\n",
    "dec.fit(x_train, y_train)\n",
    "\n",
    "# 预测准确率\n",
    "print(\"预测的准确率：\", dec.score(x_test, y_test))\n",
    "\n",
    "# 导出决策树的结构\n",
    "export_graphviz(dec, out_file=\"tree.dot\",\n",
    "                feature_names=['age', 'pclass=1st', 'pclass=2nd', 'pclass=3rd', 'female', 'male'])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-02-28T08:12:15.458558Z",
     "start_time": "2025-02-28T08:12:15.439233Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的准确率： 0.8085106382978723\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 对决策树进行参数调优"
  },
  {
   "cell_type": "code",
   "source": [
    "#调整决策树的参数\n",
    "# 分割数据集到训练集合测试集\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=4)\n",
    "# 进行处理（特征工程）特征-》类别-》one_hot编码\n",
    "dict = DictVectorizer(sparse=False)\n",
    "\n",
    "# 这一步是对字典进行特征抽取\n",
    "x_train = dict.fit_transform(x_train.to_dict(orient=\"records\"))\n",
    "x_test = dict.transform(x_test.to_dict(orient=\"records\"))\n",
    "\n",
    "# print(x_train)\n",
    "# # 用决策树进行预测，修改max_depth为10，发现提升了,min_impurity_decrease带来的增益要大于0.01才会进行划分\n",
    "dec = DecisionTreeClassifier(max_depth=7,min_impurity_decrease=0.01,min_samples_split=20)\n",
    "\n",
    "dec.fit(x_train, y_train)\n",
    "#\n",
    "# # 预测准确率\n",
    "print(\"预测的准确率：\", dec.score(x_test, y_test))\n",
    "#\n",
    "# # 导出决策树的结构\n",
    "export_graphviz(dec, out_file=\"tree1.dot\",\n",
    "                feature_names=dict.get_feature_names_out())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-11T02:55:19.080391Z",
     "start_time": "2025-01-11T02:55:19.069928Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的准确率： 0.8206686930091185\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "source": [
    "y_train.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.480266Z",
     "start_time": "2025-01-11T02:24:50.476947Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(984,)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "cell_type": "code",
   "source": [
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=4)\n",
    "# 进行处理（特征工程）特征-》类别-》one_hot编码\n",
    "dict = DictVectorizer(sparse=False)\n",
    "\n",
    "# 这一步是对字典进行特征抽取\n",
    "x_train = dict.fit_transform(x_train.to_dict(orient=\"records\"))\n",
    "x_test = dict.transform(x_test.to_dict(orient=\"records\"))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-11T02:24:50.487956Z",
     "start_time": "2025-01-11T02:24:50.481271Z"
    }
   },
   "outputs": [],
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "source": [
    "# 随机森林进行预测 （超参数调优），n_jobs充分利用多核的一个参数\n",
    "rf = RandomForestClassifier(n_jobs=-1)\n",
    "# 120, 200, 300, 500, 800, 1200,n_estimators森林中决策树的数目，也就是分类器的数目\n",
    "# max_samples  是最大样本数\n",
    "#bagging类型\n",
    "param = {\"n_estimators\": [1500,2000, 5000], \"max_depth\": [2, 3, 5, 8, 15, 25]}\n",
    "\n",
    "# 网格搜索与交叉验证\n",
    "gc = GridSearchCV(rf, param_grid=param, cv=3)\n",
    "\n",
    "gc.fit(x_train, y_train)\n",
    "\n",
    "print(\"准确率：\", gc.score(x_test, y_test))\n",
    "\n",
    "print(\"查看选择的参数模型：\", gc.best_params_)\n",
    "\n",
    "print(\"选择最好的模型是：\", gc.best_estimator_)\n",
    "\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-11T03:15:13.781538Z",
     "start_time": "2025-01-11T03:11:46.882459Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率： 0.8328267477203647\n",
      "查看选择的参数模型： {'max_depth': 3, 'n_estimators': 1500}\n",
      "选择最好的模型是： RandomForestClassifier(max_depth=3, n_estimators=1500, n_jobs=-1)\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-11T03:15:45.290936Z",
     "start_time": "2025-01-11T03:15:45.286141Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"每个超参数每次交叉验证的结果：\", gc.cv_results_)",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每个超参数每次交叉验证的结果： {'mean_fit_time': array([1.10389471, 1.44810255, 3.61165897, 1.91926042, 2.63336404,\n",
      "       6.63418492, 1.95681818, 2.65509113, 6.67638071, 2.08505575,\n",
      "       2.67859681, 6.59447591, 1.98902416, 2.77618059, 5.68396258,\n",
      "       2.02204863, 2.67197895, 6.67422001]), 'std_fit_time': array([8.22606244e-03, 1.07495338e-03, 1.80295744e-02, 6.63808456e-02,\n",
      "       9.48620834e-02, 2.23468394e-01, 9.97621278e-03, 2.00074403e-02,\n",
      "       1.00493059e-01, 4.29068463e-02, 8.00136557e-02, 2.29967552e-02,\n",
      "       1.20431048e-02, 4.29142767e-02, 1.33891788e+00, 1.00210085e-02,\n",
      "       4.94199285e-02, 1.12009843e-01]), 'mean_score_time': array([0.12761259, 0.16308641, 0.38460112, 0.20759718, 0.2921741 ,\n",
      "       0.67265026, 0.20093783, 0.2896502 , 0.67830427, 0.22817016,\n",
      "       0.2909735 , 0.66186651, 0.21123703, 0.2981077 , 0.4944977 ,\n",
      "       0.21024068, 0.27730544, 0.66817435]), 'std_score_time': array([0.00336828, 0.0085682 , 0.00152715, 0.00768164, 0.00381299,\n",
      "       0.049359  , 0.00479497, 0.00459647, 0.02873345, 0.01234872,\n",
      "       0.0145691 , 0.00590146, 0.00153051, 0.01934575, 0.12688831,\n",
      "       0.00143121, 0.00444762, 0.01470093]), 'param_max_depth': masked_array(data=[2, 2, 2, 3, 3, 3, 5, 5, 5, 8, 8, 8, 15, 15, 15, 25, 25,\n",
      "                   25],\n",
      "             mask=[False, False, False, False, False, False, False, False,\n",
      "                   False, False, False, False, False, False, False, False,\n",
      "                   False, False],\n",
      "       fill_value=999999), 'param_n_estimators': masked_array(data=[1500, 2000, 5000, 1500, 2000, 5000, 1500, 2000, 5000,\n",
      "                   1500, 2000, 5000, 1500, 2000, 5000, 1500, 2000, 5000],\n",
      "             mask=[False, False, False, False, False, False, False, False,\n",
      "                   False, False, False, False, False, False, False, False,\n",
      "                   False, False],\n",
      "       fill_value=999999), 'params': [{'max_depth': 2, 'n_estimators': 1500}, {'max_depth': 2, 'n_estimators': 2000}, {'max_depth': 2, 'n_estimators': 5000}, {'max_depth': 3, 'n_estimators': 1500}, {'max_depth': 3, 'n_estimators': 2000}, {'max_depth': 3, 'n_estimators': 5000}, {'max_depth': 5, 'n_estimators': 1500}, {'max_depth': 5, 'n_estimators': 2000}, {'max_depth': 5, 'n_estimators': 5000}, {'max_depth': 8, 'n_estimators': 1500}, {'max_depth': 8, 'n_estimators': 2000}, {'max_depth': 8, 'n_estimators': 5000}, {'max_depth': 15, 'n_estimators': 1500}, {'max_depth': 15, 'n_estimators': 2000}, {'max_depth': 15, 'n_estimators': 5000}, {'max_depth': 25, 'n_estimators': 1500}, {'max_depth': 25, 'n_estimators': 2000}, {'max_depth': 25, 'n_estimators': 5000}], 'split0_test_score': array([0.73780488, 0.73780488, 0.73780488, 0.80182927, 0.80182927,\n",
      "       0.80182927, 0.81097561, 0.81097561, 0.81097561, 0.82012195,\n",
      "       0.82012195, 0.82012195, 0.82012195, 0.82012195, 0.82012195,\n",
      "       0.81402439, 0.82012195, 0.82012195]), 'split1_test_score': array([0.82317073, 0.82621951, 0.82621951, 0.82621951, 0.82317073,\n",
      "       0.82317073, 0.81402439, 0.81402439, 0.81402439, 0.81402439,\n",
      "       0.80792683, 0.80792683, 0.81707317, 0.81402439, 0.81707317,\n",
      "       0.81707317, 0.80792683, 0.81402439]), 'split2_test_score': array([0.81707317, 0.81707317, 0.81707317, 0.82926829, 0.82926829,\n",
      "       0.82926829, 0.82317073, 0.82317073, 0.82317073, 0.79268293,\n",
      "       0.79268293, 0.79268293, 0.79573171, 0.79573171, 0.79573171,\n",
      "       0.79878049, 0.79573171, 0.79573171]), 'mean_test_score': array([0.79268293, 0.79369919, 0.79369919, 0.81910569, 0.81808943,\n",
      "       0.81808943, 0.81605691, 0.81605691, 0.81605691, 0.80894309,\n",
      "       0.80691057, 0.80691057, 0.81097561, 0.80995935, 0.81097561,\n",
      "       0.80995935, 0.80792683, 0.80995935]), 'std_test_score': array([0.0388844 , 0.03969924, 0.03969924, 0.01227952, 0.01176406,\n",
      "       0.01176406, 0.00518193, 0.00518193, 0.00518193, 0.01176406,\n",
      "       0.01122496, 0.01122496, 0.01085069, 0.01036386, 0.01085069,\n",
      "       0.00800204, 0.00995728, 0.01036386]), 'rank_test_score': array([18, 16, 16,  1,  2,  2,  4,  4,  4, 12, 14, 14,  7,  9,  7,  9, 13,\n",
      "        9])}\n"
     ]
    }
   ],
   "execution_count": 35
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
